package test3;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Aug 1, 2007
 * Time: 7:39:50 PM
 * Revision: 0
 */
public class Tokenizator
{
    private byte[] dataBuf;
    private int dataPos = 0;
    private StringBuffer strBuf;


    public Tokenizator(byte[] data)
    {
        this.dataBuf = data;
        strBuf = new StringBuffer(100);
    }

    public String getToken()
    {
        String t = peekToken();
        dataPos += t.length();
        return t;
    }

    private boolean validTokenChar(byte c)
    {
        return (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')) || ((c >= '0') && (c <= '9')) || (c == '_'));
    }

    private boolean validWhitespace(byte c)
    {
        return (c == ' ') || (c == '\n') || (c == '\t');
    }

    public String peekToken()
    {
        skipWhitespace();
        int p = dataPos;
        strBuf.delete(0, strBuf.length());

        while ((p < dataBuf.length) && (validTokenChar(dataBuf[p])))
        {
            strBuf.append((char) dataBuf[p]);
            p++;
        }

        return new String(strBuf);
    }

    public byte peekChar()
    {
        skipWhitespace();
        if (dataPos < dataBuf.length)
            return dataBuf[dataPos];

        return 0;
    }

    public byte getChar()
    {
        byte b = peekChar();
        dataPos++;
        return b;
    }

    public void skipWhitespace()
    {
        while ((dataPos < dataBuf.length) && (validWhitespace(dataBuf[dataPos])))
            dataPos++;
    }

    public void consumeTo(char c)
    {
        while ((dataPos < dataBuf.length) && (dataBuf[dataPos] != c))
            dataPos++;
    }

    public void consumeTo(String s)
    {
        while ((dataPos < dataBuf.length))
        {
            for (int i = 0; i < s.length(); i++)
                if (dataBuf[dataPos] == s.charAt(i))
                    return;

            dataPos++;
        }
    }
}
